;This file is part of C45IDL.
;
;C45IDL is free software: you can redistribute it and/or modify
;it under the terms of the GNU Lesser General Public License as published by
;the Free Software Foundation, either version 3 of the License, or
;(at your option) any later version.
;
;C45IDL is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU Lesser General Public License for more details.
;
;You should have received a copy of the GNU Lesser General Public License
;along with C45IDL.  If not, see <http://www.gnu.org/licenses/>. 

;--------------------------------------------------------------------------------------------------
; Arquivo: classificar.pro
; Objetivo:
;  Classificar a imagem de acordo com o modelo do classificador
; Entradas: 
;  Arquivo de imagem no formato retornado por imagem.pro, numero de classes numero de bandas. Usa as variaveis globais
;  que contem os vetores de media, variancia e covariancia de cada amostra
; Saidas:
;  Vetor de npixels, onde cada posicao contém o identificador da classe
; Autor: Mauricio C. M. de Paulo
; Data: 15/07/2011
;--------------------------------------------------------------------------------------------------



;This function applies the decision to pixels that are currently on idCurNode. 
pro applyDecision, pixels, imgClass, idCurNode, attrIndex, Threshold, greaterNodeId, lesserNodeId
  curSetIds=where(imgClass eq idCurNode)
  
  if curSetIds[0] ne -1 then begin
    curSet=pixels[curSetIds,*]
    curClassSet=imgClass[curSetIds]
    ;checks if this is a leaf
    if lesserNodeId eq greaterNodeId then begin
      ;if the 
      curClassSet[*]=-1*lesserNodeId
    endif else begin
      greaterIds=where(curSet[*,attrIndex-1] gt threshold)
      lesserIds=where(curSet[*,attrIndex-1] le threshold)
      if greaterIds[0] ne -1 then curClassSet[greaterIds]=greaterNodeId
      if lesserIds[0] ne -1 then curClassSet[lesserIds]=lesserNodeId
    endelse
    imgClass[curSetIds]=curClassSet
  endif
end

function classify, pixels, tree
  ;cria o array abaixo para armazenar a imagem classificada. Cada posicao do array corresponde a um pixel da imagem e
  ;o valor em cada posicao do array corresponde à classe do pixel
  imgClass = intarr(n_elements(pixels[*,0])) 
  npixels=n_elements(pixels[*,0])
  nbands=n_elements(pixels[0,*])
  for i=0, n_elements(tree[*,0])-1 do begin
    applyDecision, pixels, imgClass, i, tree[i,0], tree[i,1], tree[i,2], tree[i,3]
  endfor
  imgClass[*]=imgClass[*]*(-1)+1
  return, imgClass
end

pro writeClassImg, imgClasses, ncolumns, geoheader,outfile
  ;if the function didn't receive a file to open, open a dialog to pick one
  if ~ n_elements(outfile) then begin
    outfile = DIALOG_PICKFILE(FILTER='*.tif',/WRITE)
  endif
  npixels=n_elements(imgClasses)
  outputMatrix=reform(imgClasses-1,ncolumns,npixels/ncolumns)
  nclasses=n_elements(UNIQ(imgClasses, SORT(imgClasses)))
  color =newColorTable(nclasses)
  
  if n_tags(geoheader) eq 0 then begin
    WRITE_TIFF,outfile,outputMatrix,RED = color[0,*], GREEN = color[1,*], BLUE = color[2,*],/SHORT
  endif else begin
    WRITE_TIFF,outfile,outputMatrix,RED = color[0,*], GREEN = color[1,*], BLUE = color[2,*],GEOTIFF=geoheader, /SHORT
  endelse 
end